package com.jwthhealth_pub.user.view;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.jwthhealth_pub.R;
import com.jwthhealth_pub.app.App;
import com.jwthhealth_pub.common.base.BasetFragment;
import com.jwthhealth_pub.common.preference.PreferenceKey;
import com.jwthhealth_pub.community.view.CommunityDocApplyActivity;
import com.jwthhealth_pub.community.view.CommunityDocReportActivity;
import com.jwthhealth_pub.guardian.view.DetailsGuardianActivity;
import com.jwthhealth_pub.home.presenter.HealthPresenter;
import com.jwthhealth_pub.old.common.Constant;
import com.jwthhealth_pub.old.physicalfitness.view.IndividualActivity;
import com.jwthhealth_pub.old.sign.model.LoginModel;
import com.jwthhealth_pub.old.sign.view.SignInActivity;
import com.jwthhealth_pub.old.signutil.model.db.UserInfoDao;
import com.jwthhealth_pub.wristband.main.view.WristbandMainActivity;

import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

import static com.jwthhealth_pub.common.LogUtil.makeLogTag;


/**
 * 首页
 * Created by liuzhen on 2017/12/4.
 */

public class CenterFragment extends BasetFragment implements HealthPresenter.View {
    Unbinder unbinder;

    private static String TAG = makeLogTag(CenterFragment.class);

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view = View.inflate(getActivity(), R.layout.fragment_center, null);


        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    protected void title() {

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick(R.id.tv_user_doc_apply)
    public void onViewClicked() {
        //医师认证
        startActivity(new Intent(getActivity(), CommunityDocApplyActivity.class));
    }

    @OnClick({R.id.tv_user_docreport, R.id.tv_user_watchrelation, R.id.tv_user_doc_apply, R.id.tv_shebei
            , R.id.btn_goto_userinfo,R.id.tv_address})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_address:
                //地址管理
                startActivity(new Intent(getActivity(),EditeAddressActivity.class));
                break;
            case R.id.tv_user_docreport:
                //解毒报告
                startActivity(new Intent(getActivity(), CommunityDocReportActivity.class));
                break;
            case R.id.tv_user_watchrelation:
                startActivity(new Intent(getActivity(), DetailsGuardianActivity.class));
                break;
            case R.id.tv_user_doc_apply:
                //医师认证
                startActivity(new Intent(getActivity(), CommunityDocApplyActivity.class));
                break;
            case R.id.tv_shebei:
                startActivity(new Intent(getActivity(), WristbandMainActivity.class));
                break;
            case R.id.btn_goto_userinfo:
                startActivity(new Intent(getActivity(), MyInfoUpdateActivity.class));
                break;
        }
    }

    @OnClick(R.id.tv_loginout)
    public void loginOut() {

        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        final AlertDialog alertDialog = builder.create();
        builder.setTitle("是否退出");
//                builder.setMessage( "提示消息" ) ;
        builder.setPositiveButton("是", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                try {
                    //退出登录
                    UserInfoDao userInfoDao = UserInfoDao.getInstance();
                    boolean isclear = userInfoDao.clearUserInfo(userInfoDao.queryUserinfoModel().getId());
                    if (isclear) {
                        Log.d("IndividualInfoFragment", "is clear");
                    } else {
                        Log.d("IndividualInfoFragment", "isnt clear");
                    }

                    LoginModel.DataBean userinfoModel = UserInfoDao.getInstance().queryUserinfoModel();
                    if (userinfoModel == null) {
                        Log.d(TAG, "userinfo is null");
                    } else {
                        Log.d(TAG, "user isn't null");
                        if (userinfoModel.getId() != null) {
                            Log.d(TAG, userinfoModel.getId());
                        } else {
                            Log.d(TAG, "uid is null");
                        }
                    }

//                            userinfoModel = null;
                    App.preferenceUtil.putBoolean(PreferenceKey.CACHE_TOKEN, false);
                    Intent intent = new Intent(getActivity(), SignInActivity.class);
                    intent.putExtra(Constant.SIGN_JUMP_TOKEN, SignInActivity.FROM_INDIVIDUAL);
                    startActivity(intent);
                    alertDialog.dismiss();
                } catch (Exception e) {
                    Log.d("IndividualInfoFragment", "e:" + e);
                    alertDialog.dismiss();
                }
            }
        });

        builder.setNegativeButton("取消", null);
        builder.show();

//                AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
//                final AlertDialog alertDialog = builder.create();
//                builder.setMessage("是否退出");
//                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
//                    @Override
//                    public void onClick(DialogInterface dialogInterface, int i) {
//                        try{
//                            //退出登录
//                            boolean isclear = UserInfoDao.getInstance().clearUserInfo();
//                            userinfoModel = null;
//                            preferenceUtil.putBoolean(PreferenceKey.CACHE_TOKEN, false);
//                            startActivity(new Intent(getActivity(), SignInActivity.class));
////                mActivity.finish();
//                            alertDialog.dismiss();
//                        } catch (Exception e) {
//                            Log.d("IndividualInfoFragment", "e:" + e);
//                            alertDialog.dismiss();
//                        }
//                    }
//                });
//
//                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
//                    @Override
//                    public void onClick(DialogInterface dialogInterface, int i) {
//                        alertDialog.dismiss();
//                    }
//                });
//
//                alertDialog.show();
    }
}